package edu.aysf.controller;

import edu.aysf.entity.AccountEntity;
import edu.aysf.service.AccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;


@RestController
@RequestMapping("/account")
public class AccountController {
//    @Autowired
    private final AccountService accountService;

    public AccountController(AccountService accountService) {
        this.accountService = accountService;
    }

    // 查询指定账户,根据id
    @RequestMapping("/select/id")
    public List<AccountEntity> selectAccountById(@RequestBody Map<String,Object> map){
        // 写法2
        Integer id = Integer.parseInt(map.get("id").toString());
        return accountService.selectAccountById(id);
    }

    // 查询所有账户
    @RequestMapping("/select/all")
    public List<AccountEntity> selectAccount(){
        // 写法1
//        List<OrderEntity> list = orderService.selectOrder(id);
//        HashMap map = new HashMap();
//        map.put("list", list);
//        return map;
        // 写法2
        return accountService.selectAccount();
    }

    // 查询账户余额
    @RequestMapping("/select/money")
    public float selectMoney(@RequestBody Map<String,Object> map){
        // 写法1
//        List<OrderEntity> list = orderService.selectOrder(id);
//        HashMap map = new HashMap();
//        map.put("list", list);
//        return map;
        // 写法2
        int user_id = Integer.parseInt(map.get("user_id").toString());
        return accountService.selectMoney(user_id);
    }

    // 新增账户
    @RequestMapping("/create")
    public int createAccount(@RequestBody Map<String, Object> map){
//        int id = (int)map.get("id");
//        int user_id = (int)map.get("user_id");
//        float user_money = Float.parseFloat(map.get("user_money").toString());
        // 调用服务层的方法
        Integer id = Integer.parseInt(map.get("id").toString());
        Integer user_id = Integer.parseInt(map.get("user_id").toString());
        String source = String.valueOf(map.get("source"));
        float money = Float.parseFloat(map.get("user_money").toString());
        return accountService.createAccount(id,user_id,source,money);
    }

    // 更新账户金额
    @RequestMapping("/update")
    public int updateAccount(@RequestBody Map<String, Object> map){
        Integer user_id = Integer.parseInt(map.get("user_id").toString());
        float user_money = Float.parseFloat(map.get("user_money").toString());
        return accountService.updateAccount(user_id, user_money);
    }

    // 删除数据
    @RequestMapping("/delete")
    public int deleteAccount(@RequestBody Map<String, Object> map){
        Integer id = Integer.parseInt(map.get("id").toString());
        return accountService.deleteAccount(id);
    }
}
